package com.sunseaypw.base;

import org.apache.log4j.Logger;

public class MyLogger { 
  public static void info(String content){
    info(content,"");
  };
  public static void info(String content,String bizCatalog){
    core(LogType.info,content,bizCatalog,null);
  };
  public static void info(String content,java.lang.Throwable tb){
    core(LogType.info,content,"",tb);
  };
  public static void info(String content,String bizCatalog, java.lang.Throwable tb){
    core(LogType.info,content,bizCatalog,tb);
  };

  public static void debug(String content){
    debug(content,"");
  };
  public static void debug(String content,String bizCatalog){
    core(LogType.debug,content,bizCatalog,null);
  };
  public static void debug(String content,java.lang.Throwable tb){
    core(LogType.debug,content,"",tb);
  };
  public static void debug(String content,String bizCatalog, java.lang.Throwable tb){
    core(LogType.debug,content,bizCatalog,tb);
  };

  public static void warn(String content){
    warn(content,"");
  };
  public static void warn(String content,String bizCatalog){
    core(LogType.warn,content,bizCatalog,null);
  };
  public static void warn(String content,java.lang.Throwable tb){
    core(LogType.warn,content,"",tb);
  };
  public static void warn(String content,String bizCatalog, java.lang.Throwable tb){
    core(LogType.warn,content,bizCatalog,tb);
  };

  public static void error(String content){
    error(content,"");
  };
  public static void error(String content,String bizCatalog){
    core(LogType.error,content,bizCatalog,null);
  };
  public static void error(String content,java.lang.Throwable tb){
    core(LogType.error,content,"",tb);
  };
  public static void error(String content,String bizCatalog, java.lang.Throwable tb){
    core(LogType.error,content,bizCatalog,tb);
  };

  public static void fatal(String content){
    fatal(content,"");
  };
  public static void fatal(String content,String bizCatalog){
    core(LogType.fatal,content,bizCatalog,null);
  };
  public static void fatal(String content,java.lang.Throwable tb){
    core(LogType.fatal,content,"",tb);
  };
  public static void fatal(String content,String bizCatalog, java.lang.Throwable tb){
    core(LogType.fatal,content,bizCatalog,tb);
  };
  public static enum LogType{
    debug,info,warn,error,fatal
  }    
  static Logger logger = Logger.getLogger(MyLogger.class); 
  static void core(LogType logType,String content,String bizCatalog, java.lang.Throwable tb){         
    var appender =Logger.getRootLogger().getAppender("CATALOG");
    var f=new java.text.SimpleDateFormat("yyyyMMdd");
    var nowDate = f.format(new java.util.Date());      
    var newFile=String.format("ylog-%s%s.log", nowDate,(bizCatalog!=null&&!bizCatalog.isEmpty())?"-"+bizCatalog:"");
    if(appender!=null && appender instanceof org.apache.log4j.FileAppender){
      var fileAppender=(org.apache.log4j.FileAppender)appender;
      var oldFile = fileAppender.getFile();      
      newFile=oldFile.replaceAll("ylog-.*\\.log",newFile);
      fileAppender.setFile(newFile);
      fileAppender.activateOptions();        
    }  
    switch(logType){
      case debug:{
        logger.debug(content,tb);
        break;
      }
      case info:{
        logger.info(content,tb);
        break;
      }
      case warn:{
        logger.warn(content,tb);
        break;
      }
      case error:{
        logger.error(content,tb);
        break;
      }
      case fatal:{
        logger.fatal(content,tb);
        break;
      }
    }  
  }  
}